package com.example.yan_music.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.yan_music.R;
import com.example.yan_music.bean.ElvItem;

import java.util.ArrayList;

/**
 * <p>
 *      歌单二级列表
 * </p>
 */
public class ElvLoveAdapter extends BaseExpandableListAdapter {
    private static final String TAG = "ElvLoveAdapter";

    /**
     * group的数据源
     */
    private ArrayList<String> mGroup;

    /**
     * item的数据源
     */
    private ArrayList<ArrayList<ElvItem>> mItemList;
    private Context mContext;
    private final LayoutInflater mInflater;

    /**
     * 构造器
     *
     * @param mGroup    父项列表
     * @param mItemList 子项列表
     * @param mContext  上下文
     */
    public ElvLoveAdapter(ArrayList<String> mGroup, ArrayList<ArrayList<ElvItem>> mItemList, Context mContext, LayoutInflater mInflater) {
        this.mGroup = mGroup;
        this.mItemList = mItemList;
        this.mContext = mContext;
        this.mInflater = LayoutInflater.from(mContext);
    }

    /**
     * 获取父项个数
     *
     * @return
     */
    @Override
    public int getGroupCount() {
        return mGroup.size();
    }

    /**
     * 获取某个父项的子项个数
     *
     * @param groupPosition
     * @return
     */
    @Override
    public int getChildrenCount(int groupPosition) {
        return mItemList.get(groupPosition).size();
    }

    /**
     * 获取某个父项
     *
     * @param groupPosition
     * @return
     */
    @Override
    public Object getGroup(int groupPosition) {
        return mGroup.get(groupPosition);
    }

    /**
     * 获取某个子项
     *
     * @param groupPosition
     * @param childPosition
     * @return
     */
    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return mItemList.get(groupPosition).get(childPosition);
    }

    /**
     * 获取父项的Id
     *
     * @param groupPosition
     * @return
     */
    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    /**
     * 获取子项的Id
     *
     * @param groupPosition
     * @param childPosition
     * @return
     */
    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    /**
     * 获取父项的View
     *
     * @param groupPosition     group所在位置
     * @param isExpanded        group是否打开
     * @param convertView       容器View
     * @param parent            父布局
     * @return
     */
    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        GroupViewHolder groupViewHolder = null;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.layout_elv_group, parent, false);
            groupViewHolder = new GroupViewHolder(convertView);
            convertView.setTag(groupViewHolder);
        }else {
            groupViewHolder = (GroupViewHolder)convertView.getTag();
        }

        // group的名字
        String group = mGroup.get(groupPosition);

        // 设置默认歌单的名字
        groupViewHolder.groupName.setText(group);

        // 判断group是否打开来设置指示器的方向
        if(isExpanded){
            groupViewHolder.indicator.setBackgroundResource(R.drawable.down);
        }else {
            groupViewHolder.indicator.setBackgroundResource(R.drawable.up);
        }

        // 添加子歌单点击事件
        groupViewHolder.addSongList.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO 处理添加子歌单

            }
        });

        groupViewHolder.editSongListName.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO 点击可以编辑歌单名称

            }
        });

        return convertView;
    }


    /**
     * 获取子项的View
     *
     * @param groupPosition     所在group位置
     * @param childPosition     在group中的位置
     * @param isLastChild       是否为最后一个子项
     * @param convertView       容器view
     * @param parent            父布局
     * @return
     */
    @Override
    public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ItemViewHolder itemViewHolder = null;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.layout_elv_group_item, parent, false);
            itemViewHolder = new ItemViewHolder(convertView);
            convertView.setTag(itemViewHolder);
        }else {
            itemViewHolder = (ItemViewHolder) convertView.getTag();
        }

        // 歌单进入动画
        convertView.setAnimation(AnimationUtils.loadAnimation(mContext, R.anim.anim_rcv_item_1));


        // 拿到子项Bean
        ElvItem itemBean = mItemList.get(groupPosition).get(childPosition);
        // 歌单名
        String  songListName = itemBean.getSongListName();
        // 歌单封面资源id
        int imgRes = itemBean.getImgRes();
        // 歌单包含歌曲数目
        String songCount = itemBean.getSongCount();
        
        // 设置歌单封面
        itemViewHolder.listCover.setImageResource(imgRes);

        // 设置歌单名
        itemViewHolder.songListName.setText(songListName);

        // 设置歌单包含歌曲数目
        itemViewHolder.songCount.setText(songCount);

        // 添加子项点击时的水波纹效果，已经在设给布局的background属性了
        //itemViewHolder.rl_item.setBackgroundResource(R.drawable.ripple_elv_item);

        // 点击事件：跳转到对应歌单的详细页面
        itemViewHolder.rl_item.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(v.getContext(), "在[" + groupPosition + "]组中的第[" + childPosition + "]子项", Toast.LENGTH_SHORT).show();
            }
        });

        return convertView;
    }

    /**
     * 子项是否被选中，如果要为子项设置点击事件需要返回true
     *
     * @param groupPosition
     * @param childPosition
     * @return
     */
    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    /**
     * 父项的view搭载器
     */
    static class GroupViewHolder {
        private TextView groupName;
        private ImageView addSongList, indicator, editSongListName;

        public GroupViewHolder(View view) {
            groupName = view.findViewById(R.id.group_name);
            addSongList = view.findViewById(R.id.group_img_add);
            indicator = view.findViewById(R.id.img_open_or_close);
            editSongListName = view.findViewById(R.id.group_img_edit);
        }
    }

    /**
     * 子项的搭载器
     */
    static class ItemViewHolder {
        // 布局
        private RelativeLayout rl_item;
        // 歌单名，歌单包含的歌曲数目
        private TextView songListName;
        private TextView songCount;

        // 歌单的封面
        private ImageView listCover;

        public ItemViewHolder(View view) {
            songListName = view.findViewById(R.id.elv_tv_songlist);
            songCount = view.findViewById(R.id.elv_tv_song_count);
            listCover = view.findViewById(R.id.elv_img_songlist);
            rl_item = view.findViewById(R.id.rl_item);
        }
    }
}
